﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLNhaThuoc.DTO;

namespace QLNhaThuoc.DAL
{
    internal class MappingThuocDA : MappingDA
    {
        private const string SuccessString = "Mapping thành công";
        private const string FailureString = "Cần mapping đầy đủ các field Mã thuốc, Mã loại , Mã công ty";
        public override string mappingXMLToDB(List<ExpressionDTO> dataExpression)
        {
            List<Thuoc> lstThuoc = new List<Thuoc>();
            ThuocDA thuocDA = new ThuocDA();

            for (int i = 0; i < mapping.Data.Count; i++)
            {
                Thuoc thuoc = new Thuoc();
                int iTemp = -1;

                iTemp = exists("MaThuoc", dataExpression);
                if (iTemp != -1)
                {
                    thuoc.MaThuoc = returnEvaluated(i, dataExpression[iTemp]);
                }
                else
                {
                    return FailureString;
                }

                iTemp = exists("Ten", dataExpression);
                if (iTemp != -1)
                {
                    thuoc.Ten = returnEvaluated(i, dataExpression[iTemp]);
                }
                else
                {
                    thuoc.Ten = "";
                }

                iTemp = exists("MaLoai", dataExpression);
                if (iTemp != -1)
                {
                    string maLoai = returnEvaluated(i, dataExpression[iTemp]);
                    LoaiThuocDA loaiThuocDA = new LoaiThuocDA();
                    LoaiThuoc loaiThuoc = new LoaiThuoc {MaLoai = maLoai, Ten = ""};
                    if (!loaiThuocDA.KiemTraPK(loaiThuoc))
                    {
                        loaiThuocDA.insert(loaiThuoc);
                    }

                    thuoc.MaLoai = maLoai;
                }
                else
                {
                    return FailureString;
                }

                iTemp = exists("DonGia", dataExpression);
                if (iTemp != -1)
                {
                    thuoc.DonGia = Int32.Parse(returnEvaluated(i, dataExpression[iTemp]));
                }
                else
                {
                    thuoc.DonGia = 0;
                }

                iTemp = exists("DonVi", dataExpression);
                if (iTemp != -1)
                {
                    thuoc.DonVi = returnEvaluated(i, dataExpression[iTemp]);
                }
                else
                {
                    thuoc.DonVi = "";
                }

                iTemp = exists("MaCongTy", dataExpression);
                if (iTemp != -1)
                {
                    string maCongTy = returnEvaluated(i, dataExpression[iTemp]);
                    CongTyDA congTyDA = new CongTyDA();
                    CongTy congTy = new CongTy {MaCT = maCongTy, DiaChi = "", Ten = ""};
                    if(!congTyDA.KiemTraPK(congTy))
                    {
                        congTyDA.insert(congTy);
                    }
                    thuoc.MaCongTy = maCongTy;
                }
                else
                {
                    return FailureString;
                }

                iTemp = exists("NgaySanXuat", dataExpression);
                if (iTemp != -1)
                {
                    thuoc.NgaySanXuat = DateTime.Parse(returnEvaluated(i, dataExpression[iTemp]));
                }
                else
                {
                    thuoc.NgaySanXuat = DateTime.Now;
                }

                iTemp = exists("HanSuDung", dataExpression);
                if (iTemp != -1)
                {
                    thuoc.HanSuDung = DateTime.Parse(returnEvaluated(i, dataExpression[iTemp]));
                }
                else
                {
                    thuoc.HanSuDung = DateTime.Now;
                }

                iTemp = exists("GhiChu", dataExpression);
                if (iTemp != -1)
                {
                    thuoc.GhiChu = returnEvaluated(i, dataExpression[iTemp]);
                }
                else
                {
                    thuoc.GhiChu = string.Empty;
                }

                iTemp = exists("SoLuong", dataExpression);
                if (iTemp != -1)
                {
                    thuoc.State.SoLuong = Int32.Parse(returnEvaluated(i, dataExpression[iTemp]));
                }
                else
                {
                    thuoc.State.SoLuong = 0;
                }

                iTemp = exists("TheoChiDinh", dataExpression);
                if (iTemp != -1)
                {
                    thuoc.TheoChiDinh = bool.Parse(returnEvaluated(i, dataExpression[iTemp]));
                }
                else
                {
                    thuoc.TheoChiDinh = false;
                }

                lstThuoc.Add(thuoc);
            }

            foreach (Thuoc info in lstThuoc)
            {
                thuocDA.insert(info);
            }

            return SuccessString;
        }
    }
}
